<?php
namespace app\admin\controller;

use app\admin\model\ClassroomModel;
use cmf\controller\AdminBaseController;
use app\admin\model\School as SchoolModel;
use app\admin\model\MajorModel;
use think\db\Where;
use think\Validate;

class ClassroomController extends AdminBaseController {
    public function index() {
        extract(input());
        $where = new Where;
        if(isset($class_name) && !empty($class_name)){
            $where['class_name'] = ['like','%'.$class_name.'%'];
        }
        if (isset($school_id) && !empty($school_id)) {
            $where['school.id'] = $school_id;
            // 如果 school_id 不为空，说明本次搜索选择了学校。
            // 既然知道了学校，就可以将该学校下的专业查询出来，循环输出在搜索下拉框中，如此，就可以在搜索完成时选中选择的专业了。
            $class = MajorModel::where('school_id', $school_id)->select();
        } else {
            // 本次没有选择学校进行搜索，用一个空数组代替。
            $class = [];
        }
        if (isset($major_id) && !empty($major_id)) {
            $where['major_id'] = $major_id;
        }
        $data = ClassroomModel::with('major')
            ->where($where)
            ->order('id Desc')
            ->paginate(3,false,['query'=>input()]);
        $school = SchoolModel::all();
        $page = $data->render();
        $major = MajorModel::all();
        return $this->fetch('',[
            'data' => $data,
            'classroom'=>$class,
            'school' =>$school,
            'page' => $page,
        ]);
    }
    public function add(){
        $data = SchoolModel::all();
        return $this->fetch('',['data' => $data]);
    }
    public function addPost(){
        $data = input ();
        $add = ClassroomModel::create($data);
        if ($add) {
            return json(['code'=>1,'msg'=>'添加成功']);
        }else{
            return json(['code'=>2,'msg'=>'添加失败']);
        }
    }
    public function Getmajor(){
        $schoolId = input('school_id');
        $major =  MajorModel::where('school_id',$schoolId)
            ->select();
        $options = ["<option value=''>请选择专业</option>"];
        foreach ($major as $v){
            $options[] = "<option value='".$v['id']."'>".$v['major_name']."</option>";
        }
        return $options;
    }
    public function edit(){
        $id = input('id');
        $data = ClassroomModel::where('id',$id)->find();
        $user = SchoolModel::all();
        $School_id = MajorModel::where('id',$data['major_id'])->value('school_id');
        $major = MajorModel::where('school_id',$School_id)->select();
        $this->assign('data',$data);
        $this->assign('major',$major);
        $this->assign('school_id',$School_id);
        $this->assign('id',$id);
        $this->assign('user',$user);

        return $this->fetch();
    }
    public function Edit_major(){
        $schoolId = input('school_id');
        $major = SchoolModel::where('id',$schoolId)
            ->select();
        $options = ["<option value=''>请选择专业</option>"];
        foreach ($major as $v){
            $options[] = "<option value='".$v['id']."'>".$v['name']."</option>";
        }
        return $options;
    }
    public function editPost(){
        $data = input();
        $rule = [
            'class_name'  => 'require',
        ];
        $msg = [
            'class_name.require' => '班级名称不能为空',
        ];
        $validate = new Validate($rule, $msg);
        $result  = $validate->check($data);
        if (true !== $result) {
            $this->error($validate->getError());
        }
        $edit = ClassroomModel::where('id',$data['id'])->update($data);
        if($edit){
            return json(['code'=>1,'msg'=>'修改成功']);
        }else{
            return json(['code'=>2,'msg'=>'修改失败']);
        }
    }
    public function delete(){
        $id = input('id');
        $del  = ClassroomModel::destroy($id);
        if($del){
            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>2,'msg'=>'删除失败']);
        }
    }
    public function delAll() {
        $ids = input('ids/a');
	
        $del = ClassroomModel::destroy($ids);
        if($del){
            $this->success('删除成功','Classroom/index');
        }else{
            $this->error('删除失败','Classroom/index');
        }
    }
}